﻿namespace WordCloudClient.ViewModels.CommonVM
{
    public abstract class DataGridCommonVM : BindableBaseVM
    {
        
        #region 表格查询界面通用DelegateCommand
        public DelegateCommand ExecuteSearchDelegateCommand { get; private set; } // 执行查询  
        public DelegateCommand ExecuteResetDelegateCommand { get; private set; } // 执行重置查询条件
        public DelegateCommand<object> ExecuteAddDelegateCommand { get; private set; } 
        public DelegateCommand<object> ExecuteEditDelegateCommand { get; private set; } 
        public DelegateCommand ExecuteDeleteDelegateCommand { get; private set; }
        public DelegateCommand<object> ExecuteSelectionChangedDelegateCommand { get; private set; } // 选中
        public DelegateCommand<object> ExecuteMouseDoubleClickDelegateCommand { get; private set; } // 双击
        public DelegateCommand<object> ExecutePageIndexUpdatedDelegateCommand { get; private set; } // 执行表格跳转指定页
        public DelegateCommand ExecutePageSizeUpdatedDelegateCommand { get; private set; } // 执行表格每页最大展示数目
        public DelegateCommand ExecuteOpenCurrentFolderDelegateCommand { get; private set; }
        #endregion
        public DataGridCommonVM(SystemService systemService, WinEditHelper winEditHelper) : base(systemService, winEditHelper)
        {
            ExecuteSearchDelegateCommand = new DelegateCommand(ExecuteSearch);
            ExecuteResetDelegateCommand = new DelegateCommand(ExecuteReset);
            ExecuteAddDelegateCommand = new DelegateCommand<object>(ExecuteAdd);
            ExecuteEditDelegateCommand = new DelegateCommand<object>(ExecuteEdit);
            ExecuteDeleteDelegateCommand = new DelegateCommand(ExecuteDelete);
            ExecuteSelectionChangedDelegateCommand = new DelegateCommand<object>(ExecuteSelectionChanged);
            ExecuteMouseDoubleClickDelegateCommand = new DelegateCommand<object>(ExecuteMouseDoubleClick);
            ExecutePageIndexUpdatedDelegateCommand = new DelegateCommand<object>(ExecutePageIndexUpdated);
            ExecutePageSizeUpdatedDelegateCommand = new DelegateCommand(ExecutePageSizeUpdated);
            ExecuteOpenCurrentFolderDelegateCommand = new DelegateCommand(ExecuteOpenCurrentFolder);
            LoadData();
            BindData();
        }

        #region 通用属性
        private QueryModel _queryModel = new();
        public QueryModel QueryModel
        {
            get { return _queryModel; }
            set
            {
                _queryModel = value;
                RaisePropertyChanged(nameof(QueryModel));
            }
        }
        private object _selectObject = new();
        public object SelectObject
        {
            get { return _selectObject; }
            set
            {
                _selectObject = value;
                RaisePropertyChanged(nameof(SelectObject));
            }
        }

        private PaginationModel _paginationModel = new();
        public PaginationModel PaginationModel
        {
            get { return _paginationModel; }
            set
            {
                _paginationModel = value;
                RaisePropertyChanged(nameof(PaginationModel));
            }
        }
        private object _objectEntity = new();
        public object ObjectEntity
        {
            get { return _objectEntity; }
            set
            {
                _objectEntity = value;
                RaisePropertyChanged(nameof(ObjectEntity));
            }
        }

        private ObservableCollection<object> _objectEntities = new();
        public ObservableCollection<object> ObjectEntities
        {
            get { return _objectEntities; }
            set
            {
                _objectEntities = value;
                RaisePropertyChanged(nameof(ObjectEntities));
            }
        }
        public string EditWinAddress { get; set; } = string.Empty;
        public int TotalPages = 0;
        public int TotalCount = 0;
        #endregion

        #region 方法

        public virtual void ExecuteSearch()
        {
            QueryModel.PageIndex = 1;
            QueryModel.PageSize = PaginationModel.PageSize;
            BindData();
        }

        public virtual void ExecuteReset()
        {
            QueryModel = new();
            LoadData();
        }
        public void ExecutePageSizeUpdated()
        {
            QueryModel.PageSize = PaginationModel.PageSize;
            BindData();
        }
        public void ExecutePageIndexUpdated(object obj)
        {
            Pagination p = (Pagination)obj;
            QueryModel.PageIndex = p.PageIndex;
            QueryModel.PageSize = PaginationModel.PageSize;
            BindData();
        }
        public virtual void ExecuteAdd(object o)
        {
            DialogParameters param = new()
            {
                { "OperationType", o },
            };
            _winEditHelper.ExecuteEidt(param, EditWinAddress);
            BindData();
        }

        public virtual void ExecuteEdit(object o)
        {
            //未选中时SelectObject默认值为System.Object
            if (SelectObject.Equals(null) || SelectObject.ToString() == "System.Object")
            {
                MessageTips.InfoMessage("请选中一条数据");
                return;
            }
            //选中一条后再取消选中(点击空白处)会出现如下
            if (SelectObject is System.Windows.Controls.SelectionChangedEventArgs)
            {
                MessageTips.InfoMessage("请选中一条数据");
                return;
            }
            DialogParameters param = new()
            {
                { "OperationType", o},
                { "FristEntity", SelectObject}
            };
            _winEditHelper.ExecuteEidt(param, EditWinAddress);
            BindData();
        }

        public virtual void ExecuteDelete() { }
        public virtual void ExecuteSelectionChanged(object o)
        {
            SelectObject = o;
        }
        public virtual void ExecuteMouseDoubleClick(object o) 
        {
            //双击空白处会出现如下
            if (o is System.Windows.Input.MouseButtonEventArgs)
            {
                return;
            }
            DialogParameters param = new()
            {
                { "OperationType", "编辑"},
                { "FristEntity", o}
            };
            _winEditHelper.ExecuteEidt(param, EditWinAddress);
            BindData();
        }

        public virtual void BindData() { }
        public virtual void LoadData() { }
        public virtual void ExecuteOpenCurrentFolder() { }
        #endregion
    }
}
